<?php
include 'db.php';
/***************************************************************************
 *   Copyright (C) 2008 by Julian Cromarty                                 *
 *   j.cromarty@uea.ac.uk                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/**
 * A class to add data to the database tables.
 *
 * Rather than have the form processing script have to deal with the database
 * directly, these functions should be used so that all the processing script
 * needs to do with the data is check it and pass it on.
 *
 * @author Julian Cromarty
 */
class addData	{
/**
 * Class variable to store the database object
 */
	private var $db;
		
/**
 * The constructor.
 *
 * Gets a connection to the database and stores it in the class
 */
	function __construct()	{
		$this->db = new db();
		$this->db->connect();
	}

/**
 * The destructor
 *
 * Destroys the connection to the database to clean up
 */
	function __destruct()	{
		$this->db->close_connection();
	}

/**
 * Adds a record to the suppliers table of the database.
 *
 * @param $vars Array containing the data to put in the table
 * @return 0 on successful data entry, 1 otherwise
 */
	function addSupplier($vars)	{
		$con = $this->db->getInstance();
		$stmt = $con->prepare("INSERT INTO suppliers (supplier_name, supplier_phone, supplier_address) VALUES (?, ?, ?)");
		$stmt->bind_param('sss', $con->real_escape_string($vars['sName']), $con->real_escape_string($vars['sPhone']), $con->real_escape_string($vars['sAddress']));
		$stmt->execute();
		if ($stmt->affected_rows > 0)	{
			echo("success\n");
			return 0;
		}
		else	{
			echo "fail: ", __CLASS__, "::", __FUNCTION__, "\n";
			echo $stmt->error, "\n";
			$stmt->close();
			return 1;
		}
	}

/**
 * Adds a record to the customers table of the database.
 *
 * @param $vars Array containing the data to put in the table
 * @return 0 on successful data entry, 1 otherwise
 */
	function addCustomer($vars)	{
		$con = $this->db->getInstance();
		$stmt = $con->prepare("INSERT INTO customers (customer_name, customer_phone, customer_email, donor) VALUES (?, ?, ?, ?)");
		$stmt->bind_param('sssi', $con->real_escape_string($vars['cName']), $con->real_escape_string($vars['cPhone']), $con->real_escape_string($vars['cEmail']), $vars['cDonor']);
		$stmt->execute();
		if ($stmt->affected_rows > 0)	{
			echo("success\n");
			$stmt->close();
			return 0;
		}
		else	{
			echo "fail: ", __CLASS__, "::", __FUNCTION__, "\n";
			echo $stmt->error, "\n";
			$stmt->close();
			return 1;
		}
	}

/**
 * Adds a record to the products table of the database.
 *
 * @param $vars Array containing the data to put in the table
 * @return 0 on successful data entry, 1 otherwise
 */
	function addProduct($vars)	{
		$con = $this->db->getInstance();
		$stmt = $con->prepare("INSERT INTO products (product_name, product_price, product_supplier) VALUES (?, ?, ?)");
		$stmt->bind_param('sii', $con->real_escape_string($vars['pName']), $vars['pPrice'], $vars['pSupplier']);
		$stmt->execute();
		if ($stmt->affected_rows > 0)	{
			echo("success\n");
			$stmt->close();
			return 0;
		}
		else	{
			echo "fail: ", __CLASS__, "::", __FUNCTION__, "\n";
			echo $stmt->error, "\n";
			$stmt->close();
			return 1;
		}
	}

/**
 * Adds a record to the customer_orders table of the database.
 *
 * @param $vars Array containing the data to put in the table
 * @return 0 on successful data entry, 1 otherwise
 */
	function addCustomerOrder($vars)	{
		$con = $this->db->getInstance();
		$stmt = $con->prepare("INSERT INTO customer_orders (customer_id, products, order_date, paid) VALUES (?, ?, ?, ?)");
		$stmt->bind_param('issi', $vars['coCustomerId'], $con->real_escape_string($vars['coProducts']), $con->real_escape_string($vars['coDate']), $vars['coPaid']);
		$stmt->execute();
		if ($stmt->affected_rows > 0)	{
			echo("success\n");
			$stmt->close();
			return 0;
		}
		else	{
			echo "fail: ", __CLASS__, "::", __FUNCTION__, "\n";
			echo $stmt->error, "\n";
			$stmt->close();
			return 1;
		}
	}

/**
 * Adds a record to the product_orders table of the database.
 *
 * @param $vars Array containing the data to put in the table
 * @return 0 on successful data entry, 1 otherwise
 */
	function addProductOrder($vars)		{
		$con = $this->db->getInstance();
		$stmt = $con->prepare("INSERT INTO product_orders (supplier, products_ordered, paid, order_date) VALUES (?, ?, ?, ?)");
		$stmt->bind_param('isis', $vars['poSupplierId'], $con-real_escape_string($vars['poProducts']), $vars['poDate'], $vars['poPaid']);
		//Paid should be FALSE if not paid, else the transaction ID of the payment
		$stmt->execute();
		if ($stmt->affected_rows > 0)	{
			echo("success\n");
			$stmt->close();
			return 0;	
		}
		else	{
			echo "fail: ", __CLASS__, "::", __FUNCTION__, "\n";
			echo $stmt->error, "\n";
			$stmt->close();
			return 1;
		}
	}
}

/*if(isset($_POST['Submit']))
{
	$table = $_POST['table'];
}*/
/*
$data = new addData();
$vars['sName'] = "fred";
$vars['sPhone'] = 12445756836;
$vars['sAddress'] = "1 the place";
$data->addSupplier($vars);
$vars = null;
$vars['cName'] = "jooles";
$vars['cPhone'] = 1026454656;
$vars['cEmail'] = "jooles@jooles.com";
$vars['cDonor'] = true;
$data->addCustomer($vars);
$vars = null;
$vars['pName'] = "carrot";
$vars['pPrice'] = 10;
$vars['pSupplier'] = 53;
$data->addProduct($vars);
$vars = null;
$vars['coCustomerId'] = 7;
$vars['coProducts'] = "1";
$vars['coDate'] = "2008-10-30";
$vars['coPaid'] = true;
$data->addCustomerOrder($vars);
$data = null;
 */
?>
